
#ifndef __SDK_STREAM_ADAPTER__INCLUDE__
#define __SDK_STREAM_ADAPTER__INCLUDE__

#include "Stream.h"

#include <string>

namespace nebula { namespace sdk {

//////////////////////////////////////////////////////////////////////////
//string stream adapter
class StringIStream : public IIStream
{
public:
	StringIStream(const std::string& str);

public:
	// Read content from stream. return particular bytes read from the stream
	// return -1 if there is an error
	virtual int Read(void* ptr, unsigned int size);

private:
	const std::string& _str;
	unsigned _cursor;
};

class StringOStream : public IOStream
{
public:
	StringOStream(std::string& str);

public:
	// Read content from stream. return particular bytes read from the stream
	// return -1 if there is an error
	virtual int Write(const void* ptr, unsigned int size);

private:
	std::string& _str;
};

//////////////////////////////////////////////////////////////////////////
//stream stream adapter with length restriction
class FixLenIStreamAdapter : public IIStream
{
public:
	FixLenIStreamAdapter(IIStream* rawstream, unsigned int length);

public:
	// Read content from stream. return particular bytes read from the stream
	// return -1 if there is an error
	virtual int Read(void* ptr, unsigned int size);

private:
	IIStream* _stream;
	unsigned  _length;
	unsigned  _cursor;
};

class FixLenOStreamAdapter : public IOStream
{
public:
	FixLenOStreamAdapter(IOStream* rawstream, unsigned int length);

public:
	// Read content from stream. return particular bytes read from the stream
	// return -1 if there is an error
	virtual int Write(const void* ptr, unsigned int size);

private:
	IOStream* _stream;
	unsigned  _length;
	unsigned  _cursor;
};

}} // end of nebula::sdk

#endif //end of __SDK_STREAM_ADAPTER__INCLUDE__

